package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/kth-largest-element-in-an-array/description/?envType=study-plan-v2&envId=top-100-liked
 * 基数排序实现，自我AC
 *
 * @author yulongTian
 * @create 2024-07-14 15:59
 */
public class Test71 {
    public static void main(String[] args) {
        System.out.println(new Test71().findKthLargest(new int[]{3, 2, 1, 5, 6, 4}, 2));
    }

    public int findKthLargest(int[] nums, int k) {
        int[] index = new int[2 * (int) (Math.pow(10, 4)) + 1];
        for (int i = 0; i < nums.length; i++) {
            index[nums[i] + (int) (Math.pow(10, 4))]++;
        }
        int temp = 0;
        for (int i = index.length - 1; i >= 0; i--) {
            if (index[i] == 0) {
                continue;
            }
            if (temp <= k && k <= temp + index[i]) {
                return i - (int) (Math.pow(10, 4));
            } else {
                temp += index[i];
            }
        }
        return 0;
    }

}
